package com.hepinghospital.user.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.hepinghospital.user.base.BaseApplication
import com.hepinghospital.user.util.ToastUtil
import com.rest.business.RestProxy
import com.rest.response.DailyRecordListResponse
import io.reactivex.Observer
import io.reactivex.disposables.Disposable

class DailyRecordListViewModel : ViewModel() {

    val dataChanged = MutableLiveData<Int>()
    val refreshFail = MutableLiveData<Int>()

    val list = mutableListOf<DailyRecordListResponse>()


    fun getDailyList() {
        RestProxy.getInstance().getDailyList(object : Observer<List<DailyRecordListResponse>> {
            override fun onComplete() {
            }

            override fun onSubscribe(d: Disposable) {
            }

            override fun onNext(t: List<DailyRecordListResponse>) {
                list.clear()
                list.addAll(t)
                dataChanged.value = dataChanged.value?.plus(1)
            }

            override fun onError(e: Throwable) {
                refreshFail.value = refreshFail.value?.plus(1)
            }
        })
    }

    fun cancelDaily(outpId: String) {
        RestProxy.getInstance().cancelDaily(outpId, object : Observer<String> {
            override fun onComplete() {
            }

            override fun onSubscribe(d: Disposable) {
            }

            override fun onNext(t: String) {
                getDailyList()
//                dataChanged.value = dataChanged.value?.plus(1);
            }

            override fun onError(e: Throwable) {
                ToastUtil.onError(BaseApplication.context, e)
            }
        })
    }
}